/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tweetbotserver.robot.comms.encoder;

import java.util.HashMap;
import java.util.Map;
import tweetbotserver.robot.comms.BotMessage;
import tweetbotserver.robot.comms.encoder.encoders.StringBotMessageEncoder;

/**
 *
 * @author Mike
 */
public class BotMessageEncoderManager {

    private static Map<Class, BotMessageEncoder> encoderMap = new HashMap<Class, BotMessageEncoder>();

    static {
        encoderMap.put(String.class, new StringBotMessageEncoder());
    }

    public static byte[] encode(BotMessage object) throws Exception {
        System.out.println("Attempting to encode bot message:" + object);
        Class objectClass = object.getContent().getClass();
        byte[] output = null;

        if (encoderMap.containsKey(objectClass)) {

            BotMessageEncoder selectedEncoder = encoderMap.get(objectClass);
            output = selectedEncoder.encode(object);
        } else {
            throw new Exception("BotMessageEncoder for type:" + objectClass + " does not exist!");
        }

        return output;


    }

    public static BotMessage decode(byte[] bytes, Class objectClass) throws Exception {
        BotMessage output = null;

        if (encoderMap.containsKey(objectClass)) {

            BotMessageEncoder selectedEncoder = encoderMap.get(objectClass);
            output = selectedEncoder.decode(bytes);
            System.out.println("Generated BotMessage:" + output);
        } else {
            throw new Exception("BotMessageEncoder for type:" + objectClass + " does not exist!");
        }

        return output;


    }
}







